package com.misty.blog.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.misty.blog.service.CommentService;
import com.misty.common.bean.Result;
import com.misty.common.constants.ResultEnum;
import com.misty.dao.domain.Comment;
import com.misty.dao.dto.CommentDto;
import com.misty.dao.mapper.CommentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cthulhu
 * @since 2022-11-21
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    /**
     *
     * @Param
     * @return
     * @Description 查询该博客用户以及评论
     * @Date
     **/
    @Override
    public Result<?> blogWithComment(Integer blogId){

        //查询博客下对应评论
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<Comment>();
        lambdaQueryWrapper.eq(Comment::getBlogId,blogId);
        List<CommentDto> comment = commentMapper.selectWithUser(lambdaQueryWrapper);

        if(CollUtil.isEmpty(comment)){
            return Result.error("查询该博客用户以及评论失败！");
        }
        return Result.success("查询该博客用户以及评论成功！",comment);
    }

    /**
     *
     * @Param
     * @return
     * @Description 保存评论
     * @Date
     **/
    @Override
    public int saveComment(Comment comment){
        comment.setCreateTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        return commentMapper.insert(comment);
    }

    /**
     *
     * @Param
     * @return
     * @Description 删除评论
     * @Date
     **/
    @Override
    public int commentDelete(Integer id){
        return commentMapper.deleteById(id);
    }

}
